<?php
/**
 * 学校信息设置
 * 
 * @version 1.0
 * @date 2013-12-13
 */

class DefaultController extends FController {

    public $user_school_list;
    public $user_subject_list;
    public $user_role_list;
    public function accessRules() {
        $config = array(
            'index' => 1
        );
        return ZRole::auth("school_info", $config);
    }

    public function __common() {
        parent::init();
        if (empty($this->cLoginUser)) {
            $this->redirect($this->createUrl('/site/login'));
        }else {
            $userLoginUser = $this->cLoginUser;
            $this->user_role_list = isset($userLoginUser['cuser']['role_id']) ? $userLoginUser['cuser']['role_id'] : '';
            $this->user_school_list = isset($userLoginUser['cuser']['school']['school_id']) ? $userLoginUser['cuser']['school']['school_id'] : '';
            $this->user_subject_list = isset($userLoginUser['cuser']['school']['subject_id']) ? $userLoginUser['cuser']['school']['subject_id'] : '';
        }

        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }


    /**
	*	@ 列表显示
	*	@ yindebin 
	*	@ 2013-12-13 pm
	*
	*/
    public function actionIndex() {
        $this->__common();
        $conn = Yii::app()->db;
        $this->title = "学校信息设置";
		$s['school_code'] = trim(Yii::app()->request->getParam('school_code'));
		$s['school_name'] = trim(Yii::app()->request->getParam('school_name'));
		$s['school_nick'] = trim(Yii::app()->request->getParam('school_nick'));
		$s['edu_type'] = trim(Yii::app()->request->getParam('edu_type'));
		$s['is_display'] = trim(Yii::app()->request->getParam('is_display'));
		$s = Func::dis_input($s);
		$s = Func::fAddslashes($s);
		$where = ' AND A."is_del" = 0 ';
		$order = ' ORDER BY A."school_code" ASC';
		# 条件拼接 #
		if(Func::check_search($s['school_code'])){
			$where.= ' AND A."school_code" LIKE '."'%".$s['school_code']."%'";
		}
		if(Func::check_search($s['school_name'])){
			$where.= ' AND A."school_name" LIKE '."'%".$s['school_name']."%'";
		}
		if(!empty($s['edu_type'])){
			$where.= ' AND B."edu_id" = '."'".$s['edu_type']."'";
		}
		if(Func::check_search($s['school_nick'])){
			$where.= ' AND A."school_nick" LIKE '."'%".$s['school_nick']."%'";
		}
		if(!empty($s['is_display'])){
			$where.= ' AND A."disabled" = 1';
		}
                //权限判断
                if($this->user_role_list !=1 && !empty($this->user_school_list)){
                    $where .= ' AND a."school_id" IN ('.$this->user_school_list.')';
                }
		$select = 'SELECT A.*,B."edu_name",B."is_del"  "bis_del",B."disabled" "bdisabled" FROM "{{school}}" A'; 
		$sql = ' LEFT JOIN "{{edu_type}}" B ON A."edu_type_ids" = B."edu_id" ';
		$sql.= " WHERE 1=1".$where;

        $selectSql = $select.$sql.$order;
		$countSql = 'SELECT COUNT(*) FROM "{{school}}" A '.$sql;

		$data = ZCommon::getDaoPageList($selectSql, $countSql, 8);
		
		#  取出所有使用的学段  #
		$edu_type = array();
		$edu_type = $this->_getEduTypeInfo();

		$pageUrl = $this->createUrl('/school_info/default/index',$s);
        $this->render('index',array(
			'data'	=>$data['listDatas'],
			'pages' => $data['pages'],
			'serch'	=> $s,
			'edu_type'	=> $edu_type,
			'pageUrl' => $pageUrl
		));
    }

    //更新
    public function actionUpdate() {

        //$this->__common();
		$this->layout = '//layouts/main_window';    
		$school_id = (int)Yii::app()->request->getparam('school_id');
		$school_id = (!empty($_POST['info']['school_id'])) ? $_POST['info']['school_id'] : $school_id;
		
		$model = School::model()->findByPk($school_id);
		if(empty($model)){
			$this->redirect ( $this->createUrl ( 'index' ) );
			exit;
		}
		if(!empty($_POST['submit'])){
			$data = ZTask::dis_input($_POST['info']);
			$data['disabled'] = (!empty($data['disabled'])) ? $data['disabled'] : 0;
			$data['school_code'] = (!empty($data['school_code'])) ? strtolower($data['school_code']) : '';
			$result = School::model()->find('"school_nick" = '."'".$data['school_nick']."'".' AND "school_id" != '."'".$data['school_id']."' AND \"edu_type_ids\" = ".$data['edu_type_ids']);
			if(!empty($result)){
				if($result->is_del == 1){
					$this->frameAjaxMessage(null,'相同的上报简称已在回收站内,不可重复使用!');
				}else{
					$this->frameAjaxMessage(null,'上报简称已被使用,请更换');
				}
				
				exit;		
			}
			
			$resultcode = School::model()->find('"school_code" = '."'".$data['school_code']."'".' AND "school_id" != '."'".$data['school_id']."'");
			if(!empty($resultcode)){
				if($resultcode->is_del == 1){
					$this->frameAjaxMessage(null,'相同的学校代码已在回收站内,不可重复使用!');
				}else{
					$this->frameAjaxMessage(null,'学校代码已被使用,请更换');
				}
				//$this->frameAjaxMessage(null,'学校代码已被使用,请更换');
				exit;		
			}
           	$data['update_user'] = $this->cLoginUser['cuser']['id'];
           	$data['update_time'] = time();
			$model->attributes = $data;
			if($model->save()){
				$this->frameAjaxMessage('index');
			}else{
				$this->frameAjaxMessage(null,'更新失败');
			}
		}else{
			$edu_type = array();
			$edu_type = $this->_getEduTypeInfo();
			
			#  取地区  #
			$examObject = new ExamData();
			$region = $examObject->_getRegion();
			
			$this->render('update',array(
				'data'	=> $model->attributes,
				'edu_type'	=> $edu_type,
				'region'	=> $region,
			));
		}
    }


    /**
	*	@ 添加
	*	@ yindebin 
	*	@ 2013-12-13 pm
	*
	*/
    public function actionAdd() {
        //$this->__common();
		$this->layout = "//layouts/main_window";    
		if(!empty($_POST['submit'])){
			$data = ZTask::dis_input($_POST['info']);
			
			$data['disabled'] = (!empty($data['disabled']))?$data['disabled']:0;
			$data['school_code'] = strtolower($data['school_code']);
			
			$result = School::model()->find('"school_code" = '."'".$data['school_code']."'");
			if(!empty($result)){
				if($result->is_del == 1){
					$this->frameAjaxMessage(null,'相同的学校代码已在回收站中,不可重复使用!');
				}else{
					$this->frameAjaxMessage(null,'学校代码已被使用,请更换');
				}
				
				exit;		
			}
			
			$resultnick = School::model()->find('"school_nick" = '."'".$data['school_nick']."' AND \"edu_type_ids\" = ".$data['edu_type_ids']);
			if(!empty($resultnick)){
				if($resultnick->is_del == 1){
					$this->frameAjaxMessage(null,'相同的上报简称已在回收站中,不可重复使用!');
				}else{
					$this->frameAjaxMessage(null,'上报简称已被使用,请更换');
				}
				
				exit;		
			}
			$data['create_user'] = $this->cLoginUser['cuser']['id'];
           	$data['create_time'] = time();
           	$data['update_user'] = $this->cLoginUser['cuser']['id'];
           	$data['update_time'] = time();
            $data['is_del'] = 0;
			$model = new School();
			$model->attributes = $data;
             
			
			if($model->save()){
				$this->frameAjaxMessage('index');
			}else{
				$this->frameAjaxMessage(null,'添加失败');
			}
		}else{
			#  取出所有使用的学段  #
			$edu_type = $this->_getEduTypeInfo();
			
			#  取地区  #
			$examObject = new ExamData();
			$region = $examObject->_getRegion();
			
			$this->render('add',array(
				'edu_type'	=> $edu_type,
				'region'	=> $region,
			));
		}
    }

    //删除
    public function actionDelete() {
        $school_id = (int)Yii::app()->request->getparam('school_id');

		if(!empty($school_id)){
			$model = School::model()->findByPk($school_id);
			if(empty($model)){
				$this->redirect ( $this->createUrl ( 'index' ) );
				exit;
			}
			$sql = 'UPDATE "{{school}}" SET "is_del" = 1 WHERE "school_id" = '.$school_id;
			$query = Yii::app()->db->createCommand($sql)->execute();
			if (!empty($query)) {
				$this->redirect ( $this->createUrl ( 'index' ) );
				exit;
			} else {
				$this->frameAjaxMessage('index','删除失败');
				exit;
			}
		}else{
			$this->frameAjaxMessage('index','删除失败');
			exit;
		}
    }
	/**
	* @ 批量删除学段
	* @ yindebin
	* @ 2014-01-06 pm
	*
	*/
	public function actionDelete_all(){
		$school_id = Yii::app ()->request->getparam ( 'school_id' );
		if (! empty ( $school_id )) {
			$delete = 'UPDATE "{{school}}" SET "is_del" = 1 WHERE "school_id" IN ('.$school_id.')';
			$query = Yii::app()->db->createCommand($delete)->execute();
			echo 1;
		}else{
			echo 2;
		}
	}
	/**
	*	@ 获取学段信息
	*	@ yindebin 
	*	@ 2013-12-13 pm
	*
	*/
	public function _getEduTypeInfo(){
		#  取出所有使用的学段  #
		$eduModel = EduType::model()->findAll('"disabled" = 1 AND "is_del" = 0');
		$edu_type = array();
		
		if(!empty($eduModel)){
			foreach($eduModel as $ev){
				$edu_type[] = $ev->attributes;
			}
		}
		return $edu_type;
	}
	
	/**
	*	@ 获取地区
	*	@ yindebin 
	*	@ 2013-12-13 pm
	*
	
	public function actionAjax_region(){
			$parent_id = Yii::app()->request->getParam('parent_id');
			$examObject = new ExamData();
			$region = $examObject->_getRegion($parent_id);
			$str = "<select class='fl fontc_666' name='info[region_id]' id='region_id'><option value=''>请选择</option>";
			if(!empty($region)){
				foreach ($region as $value) {
					$str.= "<option value='".$value['id']."'>".$value['name']."</option>";
				}
			}	
			$str.= "</select>";
			echo $str;
	}
	
	*/
}